package org.ezplatform.office.forum.web.rest;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.ezplatform.cloud.common.log.LogService;
import org.ezplatform.core.common.WebUser;
import org.ezplatform.core.pagination.PropertyFilter;
import org.ezplatform.core.service.BaseService;
import org.ezplatform.core.web.controller.BaseController;
import org.ezplatform.core.web.util.WebUtils;
import org.ezplatform.core.web.view.OperationResult;
import org.ezplatform.office.forum.entity.ForumIntegral;
import org.ezplatform.office.forum.service.ForumIntegralService;
import org.ezplatform.util.StringUtils;
import org.ezplatform.util.ValidateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 论坛积分控制类
 * 2020年09月22日17:40:59
 * @author Administrator
 *
 */
@RestController
@RequestMapping(value = "/api/w/forum/integral")
public class ForumIntegralWebApiController extends BaseController<ForumIntegral, String> {
	
    private final Logger log = LoggerFactory.getLogger(ForumIntegralWebApiController.class);
    
    @Autowired
    private ForumIntegralService forumIntegralService;
    
    @Autowired
	private LogService logService;

    @Override
    protected BaseService<ForumIntegral, String> getEntityService() {
        return forumIntegralService;
    }
    
    /**
     * 初始化数据
     * @param request
     * @param model
     * @param id
     */
    @ModelAttribute
    public void prepareModel(HttpServletRequest request, Model model,
                             @RequestParam(value = "id", required = false) String id) {
        super.initPrepareModel(request, model, id);
    }
    
    /**
     * 获取用户论坛积分列表（翻页）数据
     *
     * @param request
     * @return json
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public Page<Map> list(HttpServletRequest request) {
        Pageable pageable = PropertyFilter.buildPageableFromHttpRequest(request);
        List<PropertyFilter> filter = PropertyFilter.buildFiltersFromHttpRequest(ForumIntegral.class, request);
        return forumIntegralService.findByPage(pageable, filter);
    }
    
    /**
     * 根据userId获取用户论坛积分数据
     *
     * @param request
     * @return json
     */
    @RequestMapping(value = "/viewInfo", method = RequestMethod.GET)
    @ResponseBody
    public OperationResult getIntegralViewInfo(HttpServletRequest request){
    	WebUser user = WebUtils.getCurrentUser();
    	ForumIntegral forumIntegral = forumIntegralService.getIntegralViewInfo(user.getUserId());
        return OperationResult.buildSuccessResult(forumIntegral==null?new ForumIntegral():forumIntegral);
	}
    
    /**
     * 根据积分id获取用户论坛积分数据
     *
     * @param request
     * @return json
     */
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    @ResponseBody
    public OperationResult getIntegralInfo(HttpServletRequest request){
        String id = StringUtils.null2String(request.getParameter("id"));
        return OperationResult.buildSuccessResult(forumIntegralService.findOne(id));
	}
    
    /**
     * 扣减用户论坛积分
     *
     * @param request
     * @return json
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public OperationResult editIntegralInfo(HttpServletRequest request){
        String id = StringUtils.null2String(request.getParameter("ids"));
        String[] ids = {};
        if(!"".equals(id)) {
        	ids = id.split(",");
        }
        String editScore = StringUtils.null2String(request.getParameter("editScore"));
        String mark = StringUtils.null2String(request.getParameter("mark"));
        if(!ValidateUtils.validateComm(mark)) {
        	return OperationResult.buildFailureResult("非法参数!");
        }
        return OperationResult.buildSuccessResult(forumIntegralService.editPostIntegral(ids,editScore,mark));
	}
    
}